package mscloud.config;

import com.google.code.kaptcha.text.impl.DefaultTextCreator;

import java.util.Random;

/**
 * 自定义数学验证码文本生成器
 */
public class KaptchaTextCreator extends DefaultTextCreator {

    private static final String[] OPERATORS = {"+", "-", "*"};

    @Override
    public String getText() {
        Random random = new Random();
        int x = random.nextInt(10); // 0-9
        int y = random.nextInt(10); // 0-9
        String op = OPERATORS[random.nextInt(3)];
        int result;

        switch (op) {
            case "+":
                result = x + y;
                break;
            case "-":
                result = x - y;
                break;
            case "*":
                result = x * y;
                break;
            default:
                result = 0;
        }

        return String.format("%d%s%d=?@%d", x, op, y, result);
    }
}
